﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HAUtil;

using System.IO;
using System.Diagnostics;

namespace HAGISV2
{

    /// <summary>
    /// 用于管理所有图层相关的操作
    /// </summary>
   public class LayerSystem:SubSystem
    {
       public LayerSystem(GisManager gis):base(gis)
       {

           _layerCollection = new LayerCollection();
           this._layerCollection.DefaultManager = gis;

       }


       #region 对外的事件

       //用于表示图层被选中，这里还处于试验阶段
       public event DefaultHandler LayerSelected;

       #endregion


       FMAPVIEW _fMapView=FMAPVIEW.NORMAL;

       public FMAPVIEW FMapView
       {
           get { return _fMapView; }
           set {

               if (this._fMapView!=value)
               {
                   _fMapView = value;
                   try
                   {
                       this.GisManager.CurrentMap.Layers.SuspendEvents();
                       foreach (Layer item in this._layerCollection)
                       {
                           item.ViewMode = this._fMapView;

                       }
                   }
                   catch(Exception e)
                   {
                       Debug.WriteLine("切换视图出现错误！ " + e.Message);
                   }
                   finally
                   {
                       this.GisManager.CurrentMap.Layers.ResumeEvents();
                       this.GisManager.CurrentMap.Invalidate();
                       this.GisManager.ZoomToExtent();
                   }
               }
               
               
           
           
           }
       }


       LayerCollection _layerCollection;

       /// <summary>
       /// 图层集合类
       /// </summary>
       public LayerCollection LayerCollection
       {
           get { return _layerCollection; }
         
       }



      

      



        #region 公共函数


       #region  选择相关
       public void ClearSelection()
       {
           foreach (Layer item in this._layerCollection)
           {
               if (item.IsSelectable)
               {

                   item.ClearSelection();
                   
               }
              
               
           }
 
       }

       #endregion


       public bool SaveProj()
       {

           bool b = false;

           foreach (Layer item in this.LayerCollection)
           {

               item.SaveLayer();

           }


           b= FileOperations.SaveToBinary(this.GisManager.AbsProjPathName,this._layerCollection);

          


           return b;


       }


       public bool OpenProj()
       {
           if (!File.Exists(this.GisManager.AbsProjPathName))
           {
               return false;
           }

           bool b = false;

           this._layerCollection = (LayerCollection)FileOperations.OpenFromBinary(this.GisManager.AbsProjPathName, ref b);

           this._layerCollection.DefaultManager = this.GisManager;




           if (b)
           {
               ////////////////////////////////////////测试代码/////////////////////////////////
               try
               {
                   this.GisManager.CurrentMap.Layers.SuspendEvents();

                   VectorLayer vl = null;
                   bool isFound = false;

                   foreach (Layer item in this.LayerCollection)
                   {
                       Stopwatch st = new Stopwatch();
                       st.Start();
                       item.DefaultManager = this.GisManager;

                       item.OpenLayer();
                       //item.Render();
                       if (!isFound)
                       {
                           if (item is VectorLayer)
                           {
                               vl = item as VectorLayer;
                               isFound = true;
                           }
                           if (item is GroupLayer)
                           {
                               foreach (var layer in (item as GroupLayer).Childs)
                               {
                                   if (layer is VectorLayer)
                                   {
                                       vl = layer as VectorLayer;
                                       isFound = true;
                                       break;
                                   }
                               }
                           }
                       }
                   }
                   if (isFound)
                       this.GisManager.InteractiveSystem.CurrentLayer = vl;
               }
               catch(Exception e)
               {

               }
               finally
               {
                   this.GisManager.CurrentMap.Layers.ResumeEvents();
                   this.GisManager.CurrentMap.Invalidate();

               }
           }



           //this.ReSortMapLayers();

           //this.declareLayerChanged();

           return b;

           


       }

       public bool NewProj()
       {
           this._layerCollection = new LayerCollection();
           this._layerCollection.DefaultManager = this.GisManager;
          
           return true;

       }

       public bool SaveAsProj()
       {
           throw new NotImplementedException();
       }

       public bool CloseProj()
       {

           if (this._layerCollection != null)
           {
               foreach (Layer lay in this._layerCollection)
               {
                   //单个图层要关闭一些参数                   
                   lay.StopRender();
               }

               //清空
              this._layerCollection.Clear();


             

           }



         
           ////this.Parent.CurrentMap.MapFrame.Invalidate();

           ////tree控件刷新
           //this.declareLayerChanged();

           return true;
       }

      

        #endregion




    }
}
